package in.muton.edushare.newentities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class DeptManager implements Serializable {
	private static final long serialVersionUID = 6336251942125491110L;

	private ORMInterface ormInterface;
	private IModel<Universities> univ;

	// Use this constructor to create new departments
	public DeptManager(ORMInterface ormInterface, IModel<Universities> univ) {
		this.ormInterface = ormInterface;
		this.univ = univ;
	}

	public List<String> addNewDepartments(String deptNameList) {
		List<String> stringList = new ArrayList<String>();
		if (deptNameList == null || deptNameList.compareTo("") == 0)
			return stringList;

		String[] deptArray = breakString(deptNameList);
		for (String deptName : deptArray) {
			if(!StringUtil.isValid(deptName))
				continue;
			deptName = deptName.trim();
			DepartmentsDAO dDAO = new DepartmentsDAO(ormInterface);
			stringList.add(dDAO.addDepartment(deptName, univ.getObject()));
		}
		return stringList;
	}

	public void removeFromUniv(Universities univ, Departments dept) {
		new DepartmentsDAO(ormInterface).removeDept(dept, univ);
	}
	
	public String[] breakString(String courseList) {
		Pattern p = Pattern.compile(";+");
		courseList = courseList.replace('\n',';').replace('\r', ';');
		Matcher m = p.matcher(courseList);
		return m.replaceAll(";").split(";");
	}
}
